package com.y5neko.RadiantKnightExploit.proxy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Proxy;

public class ProxySetting {
    private boolean proxyStatus = false;
    private String proxyType = "Socks";
    private String proxyHost = "127.0.0.1";
    private int proxyPort = 8080;

    private static final String PROXY_CONF = System.getProperty("user.dir") + File.separator + "config" + File.separator + "proxy_config.json";

    public void setProxyStatus(boolean proxyStatus) {
        this.proxyStatus = proxyStatus;
    }

    public void setProxyType(String proxyType) {
        this.proxyType = proxyType;
    }

    public void setProxyHost(String proxyHost) {
        this.proxyHost = proxyHost;
    }

    public void setProxyPort(int proxyPort) {
        this.proxyPort = proxyPort;
    }

    /**
     * 获取Proxy对象用以后续请求
     * @return Proxy对象
     */
    public static Proxy getProxyService() {
        Proxy proxy = Proxy.NO_PROXY;
        String json = readJSONFile();
        JSONObject jsonObject = JSONObject.parseObject(json);
        boolean proxyStatus = jsonObject.getBooleanValue("proxyStatus");
        String proxyType = jsonObject.getString("proxyType");
        String proxyHost = jsonObject.getString("proxyHost");
        int proxyPort = jsonObject.getInteger("proxyPort");
        if (proxyStatus) {
            if (proxyType.equals("Socks")) {
                return new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(proxyHost, proxyPort));
            } else if (proxyType.equals("HTTP")) {
                return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
            }
        }
        return proxy;
    }

    /**
     * 写入代理设置到配置文件
     */
    public void setProxy() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("proxyStatus", proxyStatus);
        jsonObject.put("proxyType", proxyType);
        jsonObject.put("proxyHost", proxyHost);
        jsonObject.put("proxyPort", proxyPort);
        writeJSONToFile(jsonObject);
    }

    /**
     * 获取当前代理配置信息
     * @return 代理配置信息(status, type, host, port)
     */
    public String[] getProxy(){
        String json = readJSONFile();
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            boolean proxyStatus = jsonObject.getBooleanValue("proxyStatus");
            String proxyType = jsonObject.getString("proxyType");
            String proxyHost = jsonObject.getString("proxyHost");
            int proxyPort = jsonObject.getInteger("proxyPort");
            return new String[]{String.valueOf(proxyStatus), proxyType, proxyHost, String.valueOf(proxyPort)};
        } catch (Exception e) {
            System.out.println("配置文件损坏,尝试还原");
            setProxy();
            return new String[]{"false", "Socks", "127.0.0.1", "8080"};
        }
    }

    /**
     * 读取Json文件
     * @return Json字符串
     */
    private static String readJSONFile() {
        StringBuilder stringBuilder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(PROXY_CONF))) {
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            System.out.println("读取文件失败");;
        }
        return stringBuilder.toString();
    }

    /**
     * 写入Json到文件
     * @param jsonObject Json对象
     */
    private static void writeJSONToFile(JSONObject jsonObject) {
        try (FileWriter fileWriter = new FileWriter(PROXY_CONF)) {
            // 将 JSON 对象转换为字符串并写入文件
            fileWriter.write(JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat));
        } catch (IOException e) {
            System.out.println("写入文件失败");
        }
    }
}
